from dataclasses import dataclass
from typing import Optional, List

from strategylib.models.bbc_common_enum import ExchangeType, InstType
from strategylib.models.bbc_oms_enum import Direction, OrderType, TimeInForce
from strategylib.services.bbc_lib_service import BBCLibService
from strategylib.models.bbc_oms_data import BBCBalance
from strategylib.models.bbc_oms_data import BBCOrderTrade
from strategylib.models.bbc_mds_data import Depth, InstrumentInfo
from strategylib.models.bbc_mds_data import Orderbook
from strategylib.models.bbc_mds_data import Trades
from strategylib.models.bbc_strategy_data import BBCStrategyConfig
from strategylib.models.bbc_strategy_data import BBCSchedulerNotify

import traceback
import json

from vn_local.trader.object import OrderRequest


@dataclass
class BBCStrategyTemplate(BBCLibService):
    def __init__(self, jsonval: dict):
        super().__init__(jsonval)
        self.set_balance_cb(self.on_balance)
        self.set_order_trade_cb(self.on_order_trade)
        self.set_on_depth1_cb(self.on_depth1)
        self.set_on_orderbook_cb(self.on_orderbook)
        self.set_on_public_trades_cb(self.on_public_trades)

    def on_balance(self, bbc_balance: BBCBalance):
        data_str = bbc_balance.get_string()
        print(f'[on_balance]: {data_str}')
        pass

    def on_order_trade(self, bbc_order_trade: BBCOrderTrade):
        data_str = bbc_order_trade.get_string()
        print(f'[on_order_trade]: {data_str}')
        pass

    def on_depth1(self, depth1: Depth):

        data_str = depth1.get_string()
        # print(f'[on_depth1] depth1: {data_str}')

    def on_orderbook(self, orderbook: Orderbook):
        data_str = orderbook.get_string()
        print(f'[on_orderbook] orderbook: {data_str}')


    def on_public_trades(self, public_trades: Trades):
        data_str = public_trades.get_string()
        print(f'[on_public_trade] trade: {data_str}')



if __name__ == "__main__":
    try:
        with open('./config.json', 'r') as f:
            _conf = json.load(f)
        Strategy = BBCStrategyTemplate(_conf)
        # res = Strategy.get_instrument_info(ExchangeType.BTSE, InstType.SPOT, "BTSE-USDT")
        # print(res)

        # exchangetype = ExchangeType.BTSE
        # trader = 'ccchen'
        # account="joelin"
        # inst_type = InstType.SPOT
        # inst_id = "LTC-USDT"
        # direction=Direction.BUY
        # order_type = OrderType.LIMIT
        # time_in_force = TimeInForce.GTC
        # req_price = 69.9
        # req_size = 1
        # strategy_ref = "tst"
        # strategy_order_id = "101"
        # strategy_id = "11"
        # post_only = False
        # reduce_only = False
        # recv_window = 0
        # Strategy.add_new_order(exchangetype, trader, account, inst_type, inst_id,
        # direction, order_type, time_in_force,
        #                        req_price, req_size,
        #                        strategy_ref, strategy_order_id, strategy_id, post_only, reduce_only, recv_window)

        # res = Strategy.get_orderbook(ExchangeType.BTSE, inst_type=InstType.SPOT, inst_id="LTC-USDT")
        # print(res)


    except Exception as e:
        traceback.print_exc()
